<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="需求明细">
            <PageableTable
              :loading="false"
              :table-height="200"
              :data="formItem.recipientRequires"
              :disable-pageable="true"
              border
              class="fixed-table-table"
            >
              <el-table-column
                label="序号"
                type="index"
                width="50"
                header-align="center"
                align="center"
              />
              <el-table-column v-for="column in insideColumns" :key="column.key" header-align="center" align="center" :label="column.title" :prop="column.key" :sortable="column.sortable">
                <template slot-scope="scope">
                  <column-render :render="column.render" :row="scope.row" :column="column" />
                </template>
              </el-table-column>
              <el-table-column label="操作" header-align="center" align="center" width="150">
                <template slot-scope="{row}">
                  <el-button type="text" size="mini" icon="el-icon-search" title="查询" @click="search(row)">查询</el-button>
                </template>
              </el-table-column>
            </PageableTable>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="分配明细">
            <PageableTable
              :loading="false"
              :table-height="200"
              :data="formItem.jieyong"
              :disable-pageable="true"
              border
              class="fixed-table-table"
            >
              <el-table-column
                label="序号"
                type="index"
                width="50"
                header-align="center"
                align="center"
              />
              <el-table-column v-for="column in insideColumns2" :key="column.key" header-align="center" align="center" :label="column.title" :prop="column.key" :sortable="column.sortable">
                <template slot-scope="scope">
                  <column-render :render="column.render" :row="scope.row" :column="column" />
                </template>
              </el-table-column>
              <el-table-column v-if="formItem.categories === '3'" label="配发数量" header-align="center" align="center">
                <template slot-scope="scope">
                  <el-input-number v-model="scope.row.lingyongQuantity" :min="1" size="mini" :precision="0" :max="scope.row.amount" />
                </template>
              </el-table-column>
              <el-table-column label="操作" header-align="center" align="center">
                <template slot-scope="{row}">
                  <el-button type="text" size="mini" icon="el-icon-delete" title="删除" @click="remove(row)">移除</el-button>
                </template>
              </el-table-column>
            </PageableTable>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="">
            <el-button size="mini" @click="btnCancel">取 消</el-button>
            <el-button type="primary" @click="commit">提 交</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <SelectDialog title="选择资产" :append-to-body="true" :show="assetDialogFormVisible" @on-ok="selectOk" @on-cancel="assetDialogFormVisible = false">
      <assets
        v-if="assetDialogFormVisible"
        ref="assets"
        :locked="false"
        :status-search-show="false"
        belongs="sl"
        :for-selection="true"
        :categories="formItem.categories"
        status-code="unused"
        :small-type-code="selectTypeCode"
        :type-search-show="false"
        :org-search-show="false"
      />
    </SelectDialog>
  </div>
</template>
<script>
import PageableTable from '@neu/biz-core/components/PageableTable'
import { confirmCreate as create, confirmDetail as detail } from '@neu/dcam-ui/api/jieyongSL'
import SelectDialog from '@neu/biz-core/components/SelectDialog'
import assets from '@neu/dcam-ui/views/fixedAsset/tables'
import { list as userList } from '@neu/auth-ui/api/user'
const formItem = {
  title: '',
  reason: '',
  remark: '',
  recipientRequires: [],
  createUser: '',
  orgCode: '',
  orgName: '',
  jieyong: [],
  categories: ''
}

export default {
  components: {
    PageableTable,
    SelectDialog,
    assets
  },
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      users: [],
      assetDialogFormVisible: false,
      formItem: { ...formItem },
      insideColumns: [
        { title: '资产分类', width: 300, key: 'type',
          render: (h, params) => {
            let txt = params.row.smallTypeName
            if (params.row.middleTypeName) {
              txt = params.row.middleTypeName + ' / ' + txt
            }
            if (params.row.bigTypeName) {
              txt = params.row.bigTypeName + ' / ' + txt
            }
            return h('span', txt)
          }
        },
        { title: '规格型号', width: 140, key: 'specifications' },
        { title: '需求明细', key: 'detail' },
        { title: '借用数量', width: 120, key: 'quantity' }
      ],
      insideColumns2: [
        { title: '资产名称', width: 140, key: 'name' },
        { title: '资产编号', width: 140, key: 'code' },
        { title: '资产分类', width: 300, key: 'type',
          render: (h, params) => {
            let txt = params.row.smallTypeName
            if (params.row.middleTypeName) {
              txt = params.row.middleTypeName + ' / ' + txt
            }
            if (params.row.bigTypeName) {
              txt = params.row.bigTypeName + ' / ' + txt
            }
            return h('span', txt)
          }
        },
        { title: '规格型号', width: 140, key: 'specifications' },
        { title: '序列号', key: 'serialnumber' }

      ],
      rules: {},
      selectTypeCode: ''
    }
  },
  created() {
    this.fetch()
  },
  methods: {
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = {
            ...response.data
          }
          userList({ 's_orgCodes': this.formItem.orgCode }).then(res => {
            this.users = res.data
          })
        })
      }
    },
    search(row) {
      this.selectTypeCode = row.smallTypeCode
      this.assetDialogFormVisible = true
    },
    selectOk() {
      const assets = this.$refs.assets.selections
      const assetCodes = this.formItem.jieyong.map(a => a.code)
      const difference = assets.filter(t => {
        return !assetCodes.includes(t.code)
      })
      difference.forEach(element => {
        this.formItem.jieyong.push(element)
      })
      this.assetDialogFormVisible = false
      this.selectTypeCode = null
    },
    commit() {
      this.$refs.myform.validate((valid) => {
        if (valid) {
          // 调拨资产不能为空
          if (this.formItem.jieyong.length === 0) {
            return this.$message({
              showClose: true,
              message: '分发资产为空，请填写资产明细后在提交!',
              type: 'error'
            })
          }

          const data = []

          this.formItem.jieyong.forEach(l => {
            const item = { ...l }
            item.workOrderNum = this.workOrderNum
            item.taskId = this.taskId
            item.managementOrgName = this.formItem.orgName
            item.managementOrgCode = this.formItem.orgCode
            const one = this.users.find(s => s.username === item.managementUserCode)
            item.managementUserName = one ? one.name : ''
            data.push(item)
          })

          create(data).then(res => {
            if (res.status === 'success') {
              this.$emit('cancelNewOrderForm', 'refresh')
            }
          })
        }
      })
    },
    btnCancel() {
      this.$emit('cancelNewOrderForm', 'refresh')
    },
    remove(row) {
      this.formItem.jieyong = this.formItem.jieyong.filter(it => it.id !== row.id)
    }
  }
}
</script>
